<template>
  <div id="login">
  
    <el-form
      class="form"
      ref="form"
      :model="form"
      :rules="rules"
      @keyup.enter.native="submit"
    >
      <h2
        class="text-center"
        style="margin-bottom: 20px;"
      >欢迎登陆</h2>
      <el-form-item prop="account">
        <el-input
          size="default"
          v-model="form.account"
          suffix-icon="el-icon-user"
        />
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input
          type="password"
          size="default"
          v-model="form.pwd"
          suffix-icon="el-icon-lock"
        />
      </el-form-item>
      <el-form-item>
        <el-checkbox
          label="记住密码"
          v-model="remember"
        ></el-checkbox>
        <el-checkbox
          label="自动登录"
          v-model="autoLogin"
        ></el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button
          size="default"
          type="primary"
          style="width: 100%;"
          :loading="loading"
          @click="submit"
        >登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from "@/api";
//import LoginBack from "./components/login_back"
export default {
  components:{
  //  LoginBack
  },
  data() {
    return {
      loading: false,
      remember: false,
      autoLogin: false,
      form: {
        account: "",
        pwd: ""
      },
      rules: {
        account: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        pwd: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    //
  },
  mounted() {
    this.pageInit();
  },
  methods: {
    pageInit() {
      //查找是否记住密码
      this.remember = !!this.$storage.getItem("remember", "localStorage");
      this.autoLogin = !!this.$storage.getItem("autoLogin", "localStorage");
      if (this.remember) {
        let account = this.$storage.getItem("account", "localStorage");
        console.dir(account)
        if (account) {
          this.form.account = account.email;
          this.form.pwd = account.password;
        }
      }
      if (this.autoLogin) {
        this.submit();
      }
    },
    submit() {
      this.$submit(this, "form", () => {
        this.loading = true;
        login({ email: this.form.account, password: this.form.pwd }, this.loginSuccess,err=>{
          this.loading = false;
        });
      });
    },
    loginSuccess(res) {
      if (res.code * 1 === -1) {
        this.loading = false;
        this.form.pwd = "";
      } else {
        this.loading = false;
        const expired_at = Date.now() / 1000 + res.data.expires_in
        this.$storage.setItem("token", { token: res.data.access_token, expired_at: expired_at });
        this.$storage.setItem("remember", this.remember, "localStorage");
        this.$storage.setItem("autoLogin", this.autoLogin, "localStorage");
        if (this.remember || this.autoLogin) {
          this.$storage.setItem(
            "account",
            {
              email: this.form.account,
              password: this.form.pwd
            },
            "localStorage"
          );
        }
        this.$router.replace({ name: "main" });
      }
    }
  }
};
</script>
<style lang="scss">
@import "@/styles/main/login.scss";
</style>
